/*
 * Copyright 2009 Andreas Borglin andreas.borglin@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package borge.mobile.app.jakoon;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import borge.mobile.app.jakoon.Main.ESwtExecutor;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ProgressBar;

/**
 * Form for showing progress
 * @author Andreas Borglin
 */
public class ProgressForm {

    private int totalSteps = 1;
    private Composite parent;
    private Composite top;
    private Composite progressComp;
    private Composite commandComp;
    private ESwtExecutor executor;
    
    private Label stepLabel;
    private Label textLabel;
    private ProgressBar progressBar;
    
    protected ProgressForm(ESwtExecutor exec, Composite parent) {

        executor = exec;
        this.parent = parent;
        
        top = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        top.setLayout(layout);
        
        progressComp = new Composite(top, SWT.NONE);
        layout = new GridLayout();
        layout.numColumns = 1;
        layout.marginLeft = 210;
        progressComp.setLayout(layout);
        // set layout
        
        commandComp = new Composite(top, SWT.NONE);
        layout = new GridLayout();
        layout.numColumns = 2;
        layout.marginLeft = 445;
        layout.marginTop = 185;
        commandComp.setLayout(layout);
        
        stepLabel = new Label(progressComp, SWT.NONE);
        stepLabel.setText("Unknown");
        GridData data = new GridData();
        data.horizontalAlignment = GridData.FILL;
        data.widthHint = 300;
        stepLabel.setLayoutData(data);
        
        textLabel = new Label(progressComp, SWT.NONE);
        textLabel.setText("Unknown");
        data = new GridData();
        data.horizontalAlignment = GridData.FILL;
        data.widthHint = 300;
        textLabel.setLayoutData(data);
        
        progressBar = new ProgressBar(progressComp, SWT.HORIZONTAL);
        progressBar.setMaximum(100);
        progressBar.setMinimum(0);
        data = new GridData();
        data.horizontalAlignment = GridData.BEGINNING;
        progressBar.setLayoutData(data);
        
        Button back = new Button(commandComp, SWT.PUSH);
        back.setText("Start over");
        back.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent event) {}
            public void widgetSelected(SelectionEvent event) {
                top.setVisible(false);
                executor.loadImageForm(true);
            }
        });

        Button exit = new Button(commandComp, SWT.PUSH);
        exit.setText("Exit");
        exit.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent event) {}
            public void widgetSelected(SelectionEvent event) {
                top.setVisible(false);
                executor.exit();
            }
        });
        
        progressComp.layout();
        commandComp.layout();
        top.layout();
    }
    
    protected void setVisible(boolean visible){
        if(visible){
            stepLabel.setText("Unknown");
            textLabel.setText("Unknown");
            totalSteps = 1;
            progressBar.setSelection(0);
        }
        top.setVisible(visible);
        
    }
    
    protected void initProgress(int totalSteps){
        
        if(totalSteps > -1){
            this.totalSteps = totalSteps;
            progressBar.setMaximum(totalSteps);
            progressBar.setMinimum(0);
        }
    } 
    
    protected void update(int step, String text){
        
        stepLabel.setText("Performing step " + step + " / " + totalSteps);
        textLabel.setText(text);
        if(step > -1)
            progressBar.setSelection(step);

    }
    
    protected void done(String text, String errorMsg){
        if(text.equals("true"))
            textLabel.setText("Image saved successfully!");
        else{
            textLabel.setText("Error encountered during saving!");
            MessageBox mb = new MessageBox(executor.getShell(), SWT.ICON_ERROR);
            mb.setMessage("Error: " + errorMsg);
            mb.open();
        }
    }

}
